/*
 * Created on 2005-10-6
 *
 */
package com.yl.cap.analyst.work.task;

import java.io.File;
import java.util.Date;

import org.springframework.context.ApplicationContext;

import com.yl.cap.analyst.config.ContextBean;
import com.yl.cap.analyst.report.domain.Report;
import com.yl.cap.analyst.report.service.ReportService;
import com.yl.cap.analyst.work.domain.JobLog;
import com.yl.cap.analyst.work.domain.TaskLog;
import com.yl.cap.analyst.work.service.WorkServiceImpl;
import com.yl.cap.analyst.work.task.file.BackupFile;
import com.yl.cap.analyst.work.task.file.FinishedFile;

/**
 * @author ZhangQi
 *  
 */
public class CreateReportTask implements Task
{
    private String taskFileName;
    
    private TaskInfo taskInfo;

    /**
     * @return Returns the taskInfo.
     */
    public TaskInfo getTaskInfo()
    {
        return taskInfo;
    }
    
    public CreateReportTask()
    {
        this.setTaskInfo(new XMLTaskInfo());
    }

    /**
     * @param taskInfo The taskInfo to set.
     */
    public void setTaskInfo(TaskInfo taskInfo)
    {
        this.taskInfo = taskInfo;
    }

    /**
     * @return Returns the taskFileName.
     */
    public String getTaskFileName()
    {
        return taskFileName;
    }

    /**
     * @param taskFileName The taskFileName to set.
     */
    public void setTaskFileName(String taskFileName)
    {
        this.taskFileName = taskFileName;
    }

    public TaskLog doTask(JobLog log)
    {
        Report report = null;
        WorkServiceImpl.xmlFileNames.remove(taskFileName);
        try
        {
            report = (Report) taskInfo.getTaskInfo(new File(taskFileName));
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
        report.setCreateDate(new Date());
        report.setEmailFlag("N");

        //do the job
        ApplicationContext context = ContextBean.springContext;
        ReportService service = (ReportService) context
                .getBean("reportService");
        //JobTaskDAO dao = (JobTaskDAO) context.getBean("jobTaskDAO");
        TaskLog taskLog = new TaskLog();
        taskLog.setJobLog(log);
        taskLog.setTaskName(report.getName());
        try
        {
            taskLog.setBeginTime(new Date());
            service.genReport(report, report.getName(), ContextBean.rootPath);
            FinishedFile f = new BackupFile();
            f.addressFile(taskFileName);
        }
        catch (Exception e)
        {
            taskLog.setResult("false");
            e.printStackTrace();
        }
        if (taskLog.getResult() == null)
        {
            taskLog.setResult("true");
        }
        taskLog.setEndTime(new Date());
        return taskLog;
    }

    public String getShortTaskFileName()
    {
        int pos = taskFileName.lastIndexOf(File.separator);
        return taskFileName.substring(pos+1,taskFileName.length());
    }
}
